package dao.implementations;

import dao.interfaces.NodeDao;
import engine.Node;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * An MS SQL data access object interface for nodes.
 * @author Johnny
 */
public class NodeDaoMsSql extends DaoMsSql implements NodeDao {

    public ArrayList<Node> getNodes() {
        ArrayList<Node> nodeList = new ArrayList<Node>();
        try {
            String query = "SELECT NodeID, X, Y, Name FROM Nodes";
            Statement stmt = getStatement();
            ResultSet rs = stmt.executeQuery(query);

            while (rs.next()) {
                nodeList.add(new Node(rs.getInt(1), rs.getFloat(2),
                        rs.getFloat(3), rs.getString(4)));
            }

            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(NodeDaoMsSql.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            return nodeList;
        }
    }

    public Node addNode(float x, float y, String name) {
        StringBuffer sb = new StringBuffer();

        sb.append("SET NOCOUNT ON\n");

        sb.append("INSERT INTO Nodes (X, Y, Name) ");
        sb.append("VALUES (");
        sb.append(x);
        sb.append(", ");
        sb.append(y);
        sb.append(", '");
        sb.append(name);
        sb.append("')\n");

        sb.append("SELECT SCOPE_IDENTITY()");

        Statement stmt = getStatement();
        try {
            ResultSet rs = stmt.executeQuery(sb.toString());
            rs.next();
            int newId = rs.getInt(1);
            stmt.close();
            return new Node(newId, x, y, name);
        } catch (SQLException ex) {
            Logger.getLogger(NodeDaoMsSql.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }
    }

    public void removeNode(int nodeId) {
        StringBuffer sb = new StringBuffer();

        sb.append("DELETE FROM Nodes ");
        sb.append("WHERE NodeID = ");
        sb.append(nodeId);

        Statement stmt = getStatement();
        try {
            stmt.executeUpdate(sb.toString());
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(NodeDaoMsSql.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void updateNode(Node node) {
        StringBuffer sb = new StringBuffer();
        sb.append("UPDATE Nodes SET X=");
        sb.append(node.getX());
        sb.append(", Y=");
        sb.append(node.getY());
        sb.append(", Name='");
        sb.append(node.getName());
        sb.append("' WHERE NodeID=");
        sb.append(node.getId());

        Statement stmt = getStatement();
        try {
            stmt.executeUpdate(sb.toString());
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(NodeDaoMsSql.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public Node getNode(int nodeId) {
        String query = "SELECT NodeID, X, Y, Name FROM Nodes WHERE NodeID = " +
                nodeId;
        Node node = null;

        try {
            Statement stmt = getStatement();
            ResultSet rs = stmt.executeQuery(query);
            rs.next();
            node = new Node(rs.getInt(1), rs.getFloat(2), rs.getFloat(3),
                    rs.getString(4));
            stmt.close();
        } catch (SQLException ex) {
            Logger.getLogger(NodeDaoMsSql.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return node;
    }
}
